<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
          提交
        </el-button>
        <router-link :to="'/member/department'">
          <el-button class="filter-item" style="margin-left: 10px;" v-loading="loading" type="warning">
            返回
          </el-button>
        </router-link>
      </sticky>

      <div class="createPost-main-container">
        <Warning />
        <el-row>
          <el-col :span="24">
            <div class="postInfo-container">
              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="90px" label="名称:" class="postInfo-container-item" prop="title">
                    <el-input v-model="postForm.title"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label-width="90px" label="姓名:" class="postInfo-container-item" prop="realname">
                    <el-input v-model="postForm.realname"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label-width="90px" label="联系方式:" class="postInfo-container-item" prop="mobile">
                    <el-input v-model="postForm.mobile"></el-input>
                  </el-form-item>
                </el-col>

              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="90px" label="用户名:" class="postInfo-container-item" prop="username">
                    <el-input v-model="postForm.username"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label-width="90px" label="输入密码:" class="postInfo-container-item" prop="password">
                    <el-input v-model="postForm.password" show-password></el-input>
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="90px" label="重复密码:" class="postInfo-container-item" prop="repassword">
                    <el-input v-model="postForm.repassword" show-password></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="90px" label="人员类型:" class="postInfo-container-item" prop="type">
                    <el-select v-model="postForm.type" placeholder="请选择" size="medium">
                      <el-option v-for="item in type_option" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>

            </div>
          </el-col>
        </el-row>
        <el-form-item style="margin-bottom: 40px;" label-width="70px" label="地址:">
          <el-input v-model="postForm.address" :rows="1" type="textarea" class="article-textarea" autosize placeholder="输入地址" />
        </el-form-item>

      </div>
    </el-form>
  </div>
</template>

<script>
import Warning from './Warning'
import Sticky from '@/components/Sticky' // 粘性header组件
import { fetchDepartment, createDepartment, updateDepartment } from '@/api/member/department'

const defaultForm = {
  title: '',
  realname: '',
  mobile: '', // 联系方式
  username: '', // 用户名
  password: '', // 密码
  repassword: '', //重复密码 
  address: '', // 地址
  type: 0,
  id: undefined,
}

export default {
  name: 'Detail',
  components: { Sticky, Warning },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    var repasswordValidator = (rule, value, callback) => {
      console.log(this.postForm)
      if (value !== this.postForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    }
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      type_option: [{ label: '办事员', value: 0 }, { label: '管理员', value: 1 }],
      rules: {
        // 规则  required 是否必填  message 规则不通过显示的内容  trigger 触发规则方案 blur 失去焦点时触发
        title: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的11位手机号码', trigger: 'blur' },
          {
            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            //pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号码",
          },
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 20, message: '用户名必须是5-20位', trigger: 'blur' },

        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 30, message: '密码长度在 6 到 30 个字符' },
          { pattern: /^(\w){6,20}$/, message: '只能输入6-20个字母、数字、下划线' }
        ],
        password: [
          { required: !this.isEdit, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 30, message: '密码长度在 6 到 30 个字符' },
          { pattern: /^(\w){6,20}$/, message: '只能输入6-20个字母、数字、下划线' }
        ],
        repassword: [
          { required: !this.isEdit, message: '请再次输入密码', trigger: 'blur' },
          { required: !this.isEdit, message: '请输入确认密码', trigger: 'blur' },
          { validator: repasswordValidator, trigger: 'blur' },
        ],
      },
      tempRoute: {}
    }
  },
  computed: {
  },
  created() {
    // 如果是修改，则获取api的内容
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    }

    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    fetchData(id) {
      fetchDepartment(id).then(response => {
        if (JSON.stringify(response.data.Department) != 'null') {
          console.log(response.data.Department)
          this.postForm = response.data.Department
        }
      }).catch(err => {
        console.log(err)
      })
    },

    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.isEdit) {
            updateDepartment(this.postForm).then(() => {
              this.$notify({
                title: '成功',
                message: '修改督察员成功',
                type: 'success',
                duration: 2000
              })

              this.$router.push('/member/department');
            })
          } else {
            createDepartment(this.postForm).then((res) => {

              this.$notify({
                title: '成功',
                message: '创建督察员成功',
                type: 'success',
                duration: 2000
              })
              this.$router.push('/member/department');
            })
          }

          this.postForm.status = 'published'
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}
.el-input--suffix .el-input__inner{
  padding:0 15px;
}
.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
